//
//  ChViewController.m
//  实体类
//
//  Created by 王成龙 on 2016/10/26.
//  Copyright © 2016年 wclwangchenglong. All rights reserved.
//

#import "ChViewController.h"
#import "ListData.h"
@interface ChViewController ()
{
    NSArray <ListItemData*> *an;
}

@end

@implementation ChViewController





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];
    
}

- (void)loadData
{
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/list.php"]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@",error.description);
            return;
        }
        // 获取到数据了。
        NSString *text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         ListData  *d = [[ListData  alloc]initWithDictionary:dict];
        // GCD 大中枢派发
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新界面代码
            
            if (d.errcode == 0 )
        {
            
        }
        else
        {
            NSLog(@"%@",d.errinfo);
        }
        });
    }];
    [task resume];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return an.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    ListItemData *list = an[indexPath.row];
    cell.textLabel.text = list.name;
    cell.detailTextLabel.text = list.tel;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
